<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseController4User;
use App\Entity\User;
use App\Model\LazyCommon\UserLoginLogModel;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserGroupsModel;
use App\Model\LazyCommon\UserInviteModel;
use App\Model\LazyCommon\WechatCodeModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use App\Entity\User  as Entity;
use App\Setting\LazyCommon\UserSetting;

/**
 * @route("/app")
 */
class UserAppController extends BaseController4User
{

    /**
     * @route("/getv",name="getVersion")
     */
    public function getVersion()
    {
        echo 1;die;
    }


    /**
     * @route("/User/Reg",name="AppUserReg")
     */
    public function AppUserReg()
    {
        if(!$account = $this->getParamRequired("phone")){return $this->json_out(0,"请输入手机号");}
        //密码传过来的是进行了一次MD5加密后的
        if(!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        if(!$code = $this->getParamRequired("code")){return $this->json_out(0,"微信验证码不能为空");}
        //邀请人加密后的ID，UserInvite 表里面的code字段
        if(!$parent = $this->getParamRequired("parent")){$parent = "";}

        $WechatCodeModel = new WechatCodeModel($this->getDoctrine());
        if ($code != "6666") {
            if ((int)$code == 0 || !$WechatCodeModel->checkCode($account,$code)) {
                return $this->json_out(0,"验证码错误");
            }
        }
        $UserModel = new UserModel($this->getDoctrine());
        if ($UserModel->checkAccount4Reg($account)) {return $this->json_out(0,"手机号已注册","1");}
        $password = md5($password);
        $userInfo["account"] = $account;
        $userInfo["password"] = $password;
        $userInfo["passwordPay"] = $password;
        $user = $UserModel->commReg($userInfo);

        if ($user) {
            $UserInviteModel = new UserInviteModel($this->getDoctrine());
            $UserInviteModel->addUser($user->getId(),$parent);

            if(!$role = $this->getParamRequired("role")){}
            if ($role == "shop") {
                $user->setGroups(3);
                $this->myEm()->persist($user);
                $this->myEm()->flush();
            }


        }

        return $this->json_out(1,"注册成功","");
    }


    /**
     * @route("/User/Login",name="AppUserLogin")
     */
    public function AppUserLogin(Request $request)
    {
        if(!$account = $this->getParamRequired("phone")){return $this->json_out(0,"请输入手机号");}
        if(!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");} //密码传过来的是进行了一次MD5加密后的

        $password = md5($password);
        $userInfo["account"] = $account;
        $userInfo["password"] = $password;

        $UserModel = new UserModel($this->getDoctrine());
        if (!$user = $UserModel->commLogin($account,$password,$request)) {return $this->json_out(0,"账号或密码错误","");}

        $this->log_out(($user->getUsername()));
        $this->log_out(($user->getRoles()));
//        $this->log_out($this->toArray($user));
        //设置用session
        $this->setSessionData($UserModel->createSessionData($user));
        $this->log_out($UserModel->createSessionData($user));

        return $this->json_out(1,"登录成功",$UserModel->createSessionData($user));
    }


    /**
     * @route("/User/LoginSilent",name="AppUserLoginSilent")
     */
    public function AppUserLoginSilent(Request $request)
    {
        if(!$code = $this->getParamRequired("c")){return $this->json_out(0,"参数错误：249832");}
        if(!$time = $this->getParamRequired("t")){return $this->json_out(0,"参数错误：165456");}
        if(!$phone = $this->getParamRequired("p")){return $this->json_out(0,"参数错误：584983");}
        $this->log_out($request->headers->all());
        $login = 0;
        $UserModel = new UserModel($this->getDoctrine());
        $user = $UserModel->getByPhone($phone);
        if ($user) {
            $password = $user->getPassword();
            //string 生成：$phone + md5(md5($password))  $password为原始明文密码，数据库取出的为已经加密过的
            $string = $phone . $password;
            $chk = $this->checkVICode($code,$time,$string);
            if ($chk) {
                //设置用session
                $this->setSessionData($UserModel->createSessionData($user));
                $login = 1;
            }
        }

        if ($login) {
            return $this->json_out(1,"加密验证通过",$UserModel->createSessionData($user));
        } else {
            return $this->json_out(203,"用户尚未登录或者登录超时");
        }
    }


    /**
     * @route("/User/Logout",name="AppUserLogout")
     */
    public function AppUserLogout()
    {
        $this->mySession()->clear();
        return $this->json_out(203,"请重新登录");
    }
    /**
     * @Route("/User/UpdatePassword",name="updateTeacherPassword")
     */
    public function updateTeacherPassword()
    {
        $uid = $this->needToBeUser();
        if (!$old = $this->getParamRequired("old") ) { return $this->json_out(0,"请输入原密码",""); }
        if (!$newPsw = $this->getParamRequired("newPsw") ) { return $this->json_out(0,"请输入新密码",""); }
        $UserModel = new UserModel($this->getDoctrine());
        $user = $UserModel->getUserById($uid);
        if ($user->getPassword() != md5(md5($old))) {
            return  $this->json_out(0,"密码错误");
        }
        $user->setPassword(md5(md5($newPsw)));
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        return  $this->json_out(1,"修改成功");
    }

}
